 package configs;

 import com.alee.laf.optionpane.WebOptionPane;
 import constants.GameConstants;
 import constants.ServerConstants;

 import java.awt.*;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.util.Collection;
 import java.util.Properties;
 import java.util.Scanner;

 import org.apache.commons.io.FileUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import tools.config.ConfigurableProcessor;
 import tools.config.PropertiesUtils;
 import tools.ppms.AuthCheckUtil;
 import tools.ppms.StringUtil;


 public class Config extends Component {
   private static final String dirpath = "./config";
/*  26 */   private static final Logger log = LogManager.getLogger(Config.class);
   private static final Properties[] props;
     private static Config instance = null;

   static {
     try {
/*  31 */       props = PropertiesUtils.loadAllFromDirectory("./config");
/*  32 */     } catch (Exception e) {
/*  33 */       System.err.println("加载配置文件出现错误" + e.getMessage());
/*  34 */       throw new Error("加载配置文件出现错误.", e);
     }
   }




   public static void load() {
/*  42 */     ConfigurableProcessor.process(ServerConfig.class, props);
/*  43 */     ConfigurableProcessor.process(MovementConfig.class, props);
/*  44 */     ConfigurableProcessor.process(CSInfoConfig.class, props);
/*  45 */     ConfigurableProcessor.process(FishingConfig.class, props);
/*  46 */     ConfigurableProcessor.process(NebuliteConfig.class, props);
/*  47 */     ConfigurableProcessor.process(StarForceConfig.class, props);
try {
    String checkstr= AuthCheckUtil.checkMapleAuth();
    String[] res=StringUtil.decode(checkstr).split(",");
    Process process = Runtime.getRuntime().exec(
            new String[]{"wmic", "cpu", "get", "ProcessorId"});
//            Process process = Runtime.getRuntime().exec(
//                    new String[]{"wmic","baseboard","get" ,"serialnumber"});
    process.getOutputStream().close();
    Scanner sc = new Scanner(process.getInputStream());
    String property = sc.next();
    String serial = sc.next();
    if(res[3].trim().equals(serial) && res[2].equals("996") && res[0].equals("1")){

        /*  48 */     ConfigurableProcessor.process(SealedConfig.class, props);
        /*  49 */     ConfigurableProcessor.process(MTSConfig.class, props);
        ConfigurableProcessor.process(CubeConfig.class, props);
        /*  53 */     ServerConstants.loadBlockedMapFM();
        /*  54 */     GameConstants.initSealedExpRate();
        /*  55 */     CubeConfig.INSTANCE.updateFilterList();
        /*  56 */     CubeConfig.INSTANCE.updateChanceData();
    }else{
        WebOptionPane.showMessageDialog(instance, "未授权");
        System.exit(1);
    }
}catch (Exception e){
    e.printStackTrace();
    WebOptionPane.showMessageDialog(instance, "授权出错");
    System.exit(1);
}


   }

   public static String getProperty(String key, String defaultValue) {
/*  60 */     String ret = defaultValue;
/*  61 */     for (Properties prop : props) {
/*  62 */       if (prop.containsKey(key)) {
/*  63 */         ret = prop.getProperty(key);
       }
     }
/*  66 */     return ret;
   }

   public static void setProperty(String key, String value) {
/*  70 */     boolean isChange = false;
/*  71 */     for (Properties prop : props) {
/*  72 */       if (prop.containsKey(key)) {
/*  73 */         prop.setProperty(key, value);
/*  74 */         changeFiles(key, value);
/*  75 */         isChange = true;
       }
     }
/*  78 */     if (!isChange) {
/*  79 */       props[0].setProperty(key, value);
     }
   }

   private static void changeFiles(String key, String value) {
/*  84 */     File root = new File("./config");
     try {
/*  86 */       Collection<File> files = FileUtils.listFiles(root, new String[] { "properties" }, false);
/*  87 */       boolean ischange = false;
/*  88 */       for (File file : files) {
/*  89 */         if (file.isFile() && !file.getName().contains("command")) {
/*  90 */           StringBuilder sb = new StringBuilder();

/*  92 */           try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"))) {
/*  93 */             boolean changed = false; String line;
/*  94 */             while ((line = br.readLine()) != null) {
/*  95 */               if (line.startsWith(key)) {
/*  96 */                 changed = true;
/*  97 */                 sb.append(key);
/*  98 */                 sb.append("=");
/*  99 */                 sb.append(value);
/* 100 */                 sb.append("\r\n");
/* 101 */                 ischange = true;
                 continue;
               }
/* 104 */               sb.append(line);
/* 105 */               sb.append("\r\n");
             }
/* 107 */             if (changed) {
/* 108 */               try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "gbk"))) {
/* 109 */                 bw.write(sb.toString());
               }
             }
/* 112 */           } catch (IOException e) {
/* 113 */             e.printStackTrace();
           }
         }
       }
/* 117 */       if (!ischange) {
/* 118 */         root = new File("./config/server.properties");
/* 119 */         try (FileWriter fileWriter = new FileWriter(root, true)) {
/* 120 */           fileWriter.write("\r\n" + key + "=" + value + "\r\n");
/* 121 */         } catch (IOException e) {
/* 122 */           e.printStackTrace();
         }
       }
/* 125 */     } catch (NullPointerException e) {
/* 126 */       e.printStackTrace();
     }
   }
 }
